#pragma once
#include <iostream>
#include <signal.h>
#include "TcpServer.hpp"
#include "Task.hpp"
#include "ThreadPool.hpp"
#include "Log.hpp"

class HttpServer
{
private:
    int _port;
    TcpServer *_tcp_server;
    bool _stop;

public:
    HttpServer(int port) : _port(port), _tcp_server(nullptr), _stop(false)
    {
    }
    void InitServer()
    {
        signal(SIGPIPE, SIG_IGN);
        _tcp_server = TcpServer::getinstance(_port);
    }
    void Loop()
    {
        int listen_sock = _tcp_server->Sock();
        while (!_stop)
        {
            struct sockaddr_in peer;
            socklen_t len = sizeof(peer);
            int sock = accept(listen_sock, (struct sockaddr *)&peer, &len);
            if (sock < 0)
            {
                continue;
            }
            LOG(INFO, "GET A NEW LINK");
            Task task(sock);
            ThreadPool::getinstance()->PushTask(task);
        }
    }
    ~HttpServer()
    {
    }
};